#!/usr/bin/env python3

import os
import re

def find_ff_fs_casesensitive():
    esp_idf_path = "G:\\Espressif\\frameworks\\esp-idf-v5.5.1"
    fatfs_path = os.path.join(esp_idf_path, "components", "fatfs")
    
    print(f"Searching for FF_FS_CASESENSITIVE in {fatfs_path}...")
    
    # Search all files in fatfs directory
    for root, dirs, files in os.walk(fatfs_path):
        for file in files:
            if file.endswith(".h") or file.endswith(".c") or file.endswith(".txt") or file.endswith(".md"):
                file_path = os.path.join(root, file)
                try:
                    with open(file_path, 'r', encoding='utf-8') as f:
                        content = f.read()
                        if "FF_FS_CASESENSITIVE" in content:
                            print(f"Found in {file_path}")
                            # Print context around the match
                            lines = content.split('\n')
                            for i, line in enumerate(lines):
                                if "FF_FS_CASESENSITIVE" in line:
                                    start = max(0, i-2)
                                    end = min(len(lines), i+3)
                                    for j in range(start, end):
                                        print(f"  Line {j+1}: {lines[j]}")
                                    print()
                except Exception as e:
                    print(f"Error reading {file_path}: {e}")
    
    # Also search Kconfig files
    print("\nSearching Kconfig files...")
    for root, dirs, files in os.walk(fatfs_path):
        for file in files:
            if file == "Kconfig" or file.startswith("Kconfig."):
                file_path = os.path.join(root, file)
                try:
                    with open(file_path, 'r', encoding='utf-8') as f:
                        content = f.read()
                        if "case" in content.lower() and "sensitive" in content.lower():
                            print(f"Found case-related config in {file_path}")
                            lines = content.split('\n')
                            for i, line in enumerate(lines):
                                if "case" in line.lower() and "sensitive" in line.lower():
                                    start = max(0, i-2)
                                    end = min(len(lines), i+3)
                                    for j in range(start, end):
                                        print(f"  Line {j+1}: {lines[j]}")
                                    print()
                except Exception as e:
                    print(f"Error reading {file_path}: {e}")

if __name__ == "__main__":
    find_ff_fs_casesensitive()
